<?php
/**
 * Created by yuxianjun001@icloud.com.
 * User: wuchen
 * Date: 2019/3/26
 * Time: 19:28
 */

namespace App\Services\User;


use App\Model\UserModel;
use App\Services\BaseService;

class UserService extends BaseService
{
    protected $model;

    public function __construct()
    {
        $this->model = new UserModel();
    }

    /**
     * 生成用户token
     * @param $request
     */
    public function createToken($request)
    {
        return  md5($request->name .$request->password.rand(1, 9999999).time());
    }

     /**
     *平台后台增加用户
     * @param $request
     * @return array
     */
    public function addUser($request)
    {
        $user  = UserModel::where(
            ['name'=> $request->name,'deleted_at'=>null])
            ->first();

        if (!empty($user)) {
            return [
                'code' => 500,
                'msg'  => '用户存在',
                'data' => ['api_token'=>$user->api_token],
            ];
        }

        $token=$this->createToken($request);
        $data = [
            'name'     => $request->name,
            'phone'    => $request->phone,
            'password' => md5($request->password),
            'type'     => $request->type ?: 1,//用户类型，1-管理后台用户，2-商家用户，3-买家用户，4，骑手用户，默认-1
            'api_token'    =>$token,
        ];

        $res = $this->model->save($data);

        if (!empty($res)) {
            return [
                'code' => 200,
                'msg'  => '新增用户成功',
                'data' => ['api_token'=>$token],
            ];

        }

    }

    /**
     * 平台后台修改用户
     * @param $request
     * @return array
     */
    public function editUser($request)
    {
        $user = UserModel::where('id', $request->id)->first();

        if (empty($user)) {
            return [
                'code' => 500,
                'msg'  => '用户不存在',
                'data' => ['id'=>$request->id],
            ];
        }

        $data = [
            'name'     => $request->name,
            'phone'    => $request->phone,
            'password' => md5($request->password),
        ];

        $res = $this->model->where('id',$request->id)->update($data);
        //dd($res);
        if (!empty($res)) {
            return [
                'code' => 200,
                'msg'  => '修改用户成功',
                'data' => '',
            ];
        }

    }

    /**
     * 平台后台删除用户
     * @param $request
     */
    public function delUser($request)
    {
        $user = UserModel::where('id', $request->id)->first();
        if (empty($user)) {
            return [
                'code' => 500,
                'msg'  => '用户不存在',
                'data' => $request->id,
            ];
        }

        $res = $this->model->where('id', $request->id)->delete();

       if ($res) {
            return [
                'code' => 200,
                'msg'  => '用户删除成功',
                'data' => $user->id,
            ];
        }

    }

    /**
     * 平台后端，商家后端登录
     * @param $request
     */
    public function loginByNameAndPwd($request)
    {

        $user = UserModel::where('name', $request->name)->first();
        if (empty($user)) {
            return [
                'code' => 404,
                'msg'  => '该账号不存在！',
                'data' => '',
            ];
        }

        if ($user->password == md5($request->password)) {
            return [
                'code' => 200,
                'msg'  => '登录成功',
                'data' => ['user_info' => ['id'=>$user->id,'name'=>$user->name,'api_token'=>$user->api_token]],
            ];
        }

    }


    /**
     * 通过 token，验证用户合法性
     * @param $request
     */
    public function checkUser($request)
    {
        $user = UserModel::where('name', $request->name)->orWhere('phone', $request->phone)->where('deleted_at', null)->first();
        if (!empty($user)) {
            if ($user->api_token == $request->token) {
                return $this->response(['api_token' => $user->api_token], '登录合法');
            }
            return $this->response('', 'api_token 验证失败', 500);
        }
        return $this->response('', '用户不存在', 404);

    }

    /**
     * 通过uid或者phone查询用户信息
     * @param $request
     */
    public function userInfo($request)
    {
        $res = UserModel::where('id', $request->id)->orWhere('phone', $request->phone)->first();
        if (!empty($request)) {
            return $this->response($res->toArray(), '查询成功');
        }
        return $this->response($res->toArray(), '查询失败', 404);
    }


}